import {
    addIncidentals,
    deleteById,
    getById,
    getIncidentalsAllList,
    getListForPage,
    updateIncidentals
} from '../../services/quotationSupport/incidentals';
import {message} from 'antd';
import Constants from '../../utils/Constants'
const R = require('ramda');

export default {
    namespace: 'incidentals',
    state:{
        page: {},
        list: [],
        incidentalsList: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state,{ payload }){
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页获取供应商费用项
         * 
         * @param call
         * @param put
         * 
         */
        *getListForPage({ payload }, { call, put }){
            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

        /**
         * 根据id获取供应商费用项
         * 
         * @param id
         */
        *getById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                    if (payload.onSuccess) payload.onSuccess();
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
            }
        },

        /**
         * 新增供应商费用项
         * 
         * @param call
         */
        *addIncidentals({ payload },{ call }){
            const res = yield call(addIncidentals, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("新增杂费成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

        /**
         * 修改供应商费用项
         * 
         * @param call
         * @param put
         * @param select
         */
        *updateIncidentals({ payload },{ call, put }){
            const res = yield call(updateIncidentals, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("更新杂费成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

        /**
         * 根据ID删除供应商费用项
         * 
         * @param id
         */
        *deleteById({ payload },{ call }){
            const res = yield call(deleteById,{ ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("删除杂费成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

        /**
         * 查询杂费List
         *
         */
        *getIncidentalsAllList({ payload },{ call, put }){
            const res = yield call(getIncidentalsAllList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        incidentalsList: R.isNil(res.data.content) ? [] : res.data.content
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        }
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};